/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.chl.vrangogym.persistence.entities;

import edu.chl.vrangogym.persistence.entities.values.ContactInfo;
import edu.chl.vrangogym.persistence.entities.enums.Permissions;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import javax.persistence.CascadeType;
import javax.persistence.Embedded;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.persistence.OneToMany;

/**
 *
 * @author habbbe
 * 
 * @revidedBy Ulken (2011-10-16)
 */
@Entity(name = "gym_user")
public class User implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    private String userName;
    private String password;
    @Embedded
    private ContactInfo contactInfo;
    private int credits;
    @OneToMany(mappedBy = "booker", cascade = CascadeType.ALL)
    private Collection<Booking> bookings;
    @Enumerated(EnumType.STRING)
    private Permissions permissions;

    /**
     * Instantiate a new user
     */
    public User() {
    }

    /**
     * Instantiate a new user
     * @param userName the user name
     * @param password the password
     */
    public User(String userName, String password) {
        this(userName, password, null, 0, new ArrayList<Booking>(), null);

    }

    /**
     * Instantiate a new user
     * @param userName the user name
     * @param password the password
     * @param contactInfo the contact info
     * @param credits the credits
     * @param bookings the bookings
     * @param permissions the permissions for the user
     */
    public User(String userName, String password, ContactInfo contactInfo, int credits, Collection<Booking> bookings, Permissions permissions) {
    this.userName = userName;
    this.password = password;
    this.contactInfo = contactInfo;
    this.credits = credits;
    this.bookings = bookings;
    this.permissions = permissions;
    }
    
    /**
     * Immutable: Should not be able to alter booking list once instantiated
     */
    public Collection<Booking> getBookings() {
        return bookings;
    }

    public ContactInfo getContactInfo() {
        return contactInfo;
    }

    public void setContactInfo(ContactInfo contactInfo) {
        this.contactInfo = contactInfo;
    }

    public int getCredits() {
        return credits;
    }

    public void setCredits(int credits) {
        this.credits = credits;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Permissions getPermissions() {
        return permissions;
    }

    public void setPermissions(Permissions permissions) {
        this.permissions = permissions;
    }

    /**
     * Immutable: Should not be able to alter username (primary key)
     */
    public String getUserName() {
        return userName;
    }

    public void addBooking(Booking booking) {
        bookings.add(booking);
    }

    public void removeBooking(Booking booking) {
        bookings.remove(booking);
    }

    /*
    @PreRemove
    public void onRemove() {
    bookings = null;
    }
     */
    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final User other = (User) obj;
        if ((this.userName == null) ? (other.userName != null) : !this.userName.equals(other.userName)) {
            return false;
        }
        return true;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 59 * hash + (this.userName != null ? this.userName.hashCode() : 0);
        return hash;
    }

    @Override
    public String toString() {
        return "edu.chl.vrangogym.persistence.entities.User[ id=" + userName + " ]";
    }
}
